package demo1;

//牛客网:走迷宫

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static int[] xx = {1,-1,0,0};
    public static int[] yy = {0,0,1,-1};

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt();
        int xs = in.nextInt()-1;
        int ys = in.nextInt()-1;
        int xt = in.nextInt()-1;
        int yt = in.nextInt()-1;
        String[] map = new String[n];
        for(int i = 0; i<n; i++) {
            map[i] = in.next();
        }

        int ret = 0;
        boolean flag = false;
        boolean[][] st = new boolean[n][m];

        Queue<int[]> queue = new LinkedList<>();
        queue.add(new int[]{xs,ys});
        st[xs][ys] = true;

        while(!queue.isEmpty()) {
            int size = queue.size();
            while(size-- > 0) {
                int[] p = queue.poll();
                int x = p[0];
                int y = p[1];
                if(x == xt && y == yt) {
                    flag = true;
                    break;
                }

                for(int j = 0; j<4; j++) {
                    int dx = x + xx[j];
                    int dy = y + yy[j];
                    if(dx >= 0 && dx < n && dy >= 0 && dy < m && map[dx].charAt(dy)=='.' && !st[dx][dy]) {
                        st[dx][dy] = true;
                        queue.add(new int[]{dx,dy});
                    }
                }
            }
            if(flag) {
                break;
            }
            ret ++;
        }

        if(flag) {
            System.out.println(ret);
        }else {
            System.out.println(-1);
        }
    }
}